{% extends "repo/repo.html" %}
{% block css %}
<link href="/static/less/highlight/github.css" rel="stylesheet" type="text/css">
{% endblock %}

{% block subcontainer %}
<div class="header clearfix"></div>
<div class="files bubble">
  <div class="file-list table-container" id="source">
    <div class="theme-selector">
      {% include "repo/branch_nav.html" %}
      <div class="group clearfix">
        <span class="current-branch-name">
          <a href="/{{user_name}}/{{repo_name}}/{%if current == 'index' or current == 'tree' or current == 'blob' %}tree{%endif%}{%if current == 'commits' %}commits{%endif%}{%if current == 'refs_graph' %}refs/graph{%endif%}/{{refs}}/">{{repo_name}}</a><span class="divider">/</span>
          <span id="repo-path"></span>
        </span>
        <span class="views pull-right">
          <a href="/{{user_name}}/{{repo_name}}/commits/{{refs}}/{{path}}"><em>提交</em></a>
          <a href="/{{user_name}}/{{repo_name}}/raw/blob/{{refs}}/{{path}}"><em>原始文件</em></a>
        </span>
      </div>
    </div>

    <div id="i_source_code">
      {% if not blob and blob != '' %}
      <p class="alert alert-error">没有源代码、二进制文件，或者没有查看源代码权限，半公开和私有项目需要申请成为成员才能查看源代码</p>
      {% else %}
        {% if is_markdown %}
          <div id="id_markdown_content" class="markdown hide">{{ blob }}</div>
        {% else %}
          {{ blob|safe }}
        {% endif %}
      {% endif %}
    </div>          
  </div>

</div>
{% endblock %}
{% block subjs %}
{% if is_markdown %}
<script src="/static/js/app/markdown/marked.js"></script>
<script src="/static/js/app/caja/html-css-sanitizer-minified.js"></script>
{% endif %}
<script>
  $(function(){
    set_path_href = function() {
      var base_href_path = '/{{user_name}}/{{repo_name}}/tree/{{refs}}/';
      var path = '{{path}}';
      if(path == '.') {
        return;
      }
      var paths = path.split('/');
      path_href_html = '';
      for(x in paths) {
        subpath = paths[x];
        if(subpath == '') {
          break;
        }
        base_href_path = (base_href_path + subpath + '/');
        if(path.substr(-1) === "/") {
          if(x != (paths.length-2)) {
            path_href_html = (path_href_html + ' <a href="' + base_href_path + '">' + subpath + '</a> /');
          } else {
            path_href_html = (path_href_html + ' ' + subpath + ' /');
          }
        } else {
          if(x != (paths.length-1)) {
            path_href_html = (path_href_html + ' <a href="' + base_href_path + '">' + subpath + '</a> /');
          } else {
            path_href_html = (path_href_html + ' ' + subpath);
          }
        }
      }
      $('#repo-path').html(path_href_html);
    };
    set_path_href();
    {% if is_markdown %}
      marked.setOptions({ gfm: true, tables: true, breaks: false, pedantic: false, sanitize: false, smartLists: true, smartypants: false, langPrefix: 'lang-' });
    var html = $('#id_markdown_content').html();
    html = $('<div/>').html(html).text();
    var sanitized_html = html_sanitize(marked(html), function(url) { return url; }, function(id) { return id; })
      $('#id_markdown_content').html(sanitized_html);
      $('#id_markdown_content').show();
    {% endif %}
  });
</script>
{% endblock %}
